<?php
/**
 * Created by PhpStorm.
 * User: zhj
 * Date: 2019/4/12
 * Time: 10:55
 */
namespace app\admin\controller;

use app\admin\logic\Fields;
use app\common\model\Region;

/**
 * 数据控制器
 */
class Userfile extends AdminBase
{

    /**
     * 数据列表
     */
    public function userfileList()
    {

        $search_data = input('param.search_data');
        $where['a.name'] = ['like',"%$search_data%"];
        $data = $this->logicUserfile->getUserfileList($where, 'a.*,m.nickname,c.name as category_name,d.path');

        $this->assign('list', $data);

        $this->assign('userfile_category_list', $this->logicUserfile->getUserfileCategoryList([], 'id,name', '', false));
        return $this->fetch('userfile_list');
    }

    /**
     * 数据添加
     */
    public function userfileAdd()
    {

        $this->userfileCommon();
        return $this->fetch('userfile_edit');
    }

    /**
     * 数据编辑
     */
    public function userfileEdit()
    {

        $this->userfileCommon();

        $info = $this->logicUserfile->getUserfileInfo(['a.id' => $this->param['id']], 'a.*,c.name as category_name');

        $this->assign('info', $info);

        return $this->fetch('userfile_edit');
    }

    /**
     * 数据添加与编辑通用方法
     */
    public function userfileCommon()
    {
        if(IS_POST && $this->param['file_id']){
            $this->param['userfile_id'] = $this->param['file_id'];
            unset($this->param['file_id']);
        }
        IS_POST && $this->jump($this->logicUserfile->userfileEdit($this->param));

        $this->assign('userfile_category_list', $this->logicUserfile->getUserfileCategoryList([], 'id,name', '', false));
    }

    /**
     * 数据分类添加
     */
    public function userfileCategoryAdd()
    {

        IS_POST && $this->jump($this->logicUserfile->userfileCategoryEdit($this->param));

        return $this->fetch('userfile_category_edit');
    }

    /**
     * 数据分类编辑
     */
    public function userfileCategoryEdit()
    {

        IS_POST && $this->jump($this->logicUserfile->userfileCategoryEdit($this->param));

        $info = $this->logicUserfile->getUserfileCategoryInfo(['id' => $this->param['id']]);

        $this->assign('info', $info);

        return $this->fetch('userfile_category_edit');
    }

    /**
     * 数据分类列表
     */
    public function userfileCategoryList()
    {

        $this->assign('list', $this->logicUserfile->getUserfileCategoryList());

        return $this->fetch('userfile_category_list');
    }

    /**
     * 数据分类删除
     */
    public function userfileCategoryDel($id = 0)
    {

        $this->jump($this->logicUserfile->userfileCategoryDel(['id' => $id]));
    }

    /**
     * 数据状态设置
     */
    public function setStatus()
    {

        $this->jump($this->logicAdminBase->setStatus('Userfile', $this->param));
    }

    /**
     * 修改列表名
     */
    public function editColName()
    {
        $data = input('post.');
        if(!empty($data['id']) && !empty($data['data'])){
            $res = (new Fields())->where('id',$data['id'])->setField('name',$data['data']);
            if($res){
                $this->apiReturn(1,[],'修改成功~');
            }else{
                $this->apiReturn(1,[],'修改成功~');
            }
        }
    }
    /***
     * 导入数据
     */
    public function importData()
    {
        $url = 'D:\work\PHPTutorial\WWW\jiajiang\public\upload\file\customer.xlsx';
        $data = get_excel_data($url,2,0,1);
        $fields = ['name','phone','operator','product','province','city','county','hometown','street','describe','identification','update_date'] ;
        $list  =  [] ;
        foreach ($data as $k=>$value){
            foreach ($value as $j=>$v){
                $list[$k][$fields[$j]] = $v;
                $list[$k]['member_id'] = MEMBER_ID;
            }
        }
        $res = (new \app\common\model\Userfile())->saveAll($list);
        if($res){
            echo 11;exit;
        }else{
            echo 'false';exit;
        }
    }
}